$(function() {
    var layer = layui.layer
    var form = layui.form
    initArtCateList()

    function initArtCateList() {
        $.ajax({
            method: 'GET',
            url: 'http://api-breakingnews-web.itheima.net/my/article/cates',
            headers: {
                Authorization: localStorage.getItem('token')
            },
            success: function(res) {
                if (res.status !== 0) {
                    return layer.msg('获取文章分类列表失败！')
                }
                console.log(res);

                var htmlTer = template('tpl_tbody', res)
                $('tbody').html(htmlTer)
            }
        })
    }

    var indexAdd = null;

    // 为添加类型按钮绑定点击事件
    $('#btnCategory').on('click', function() {
        indexAdd = layer.open({
            type: 1,
            area: ['500px', '300px'],
            title: '添加文章分类',
            content: $('#popup').html()
        });
    })

    // 通过代理的形式 为form-add 表单绑定submit事件
    $('body').on('submit', '#form-add', function(e) {
        e.preventDefault()
        $.ajax({
            method: 'POST',
            url: 'http://api-breakingnews-web.itheima.net/my/article/addcates',
            headers: {
                Authorization: localStorage.getItem('token')
            },
            data: $(this).serialize(),
            success: function(res) {
                if (res.status !== 0) {
                    return layer.msg('新增分类失败！')
                }
                initArtCateList()
                layer.msg('新增分类成功！')

                // 根据索引，关闭对应的弹出框
                layer.close(indexAdd)
            }
        })
    })

    var indexEdit = null;
    $('body').on('click', '#btn-edit', function() {
        indexEdit = layer.open({
            type: 1,
            area: ['500px', '300px'],
            title: '编辑文章分类',
            content: $('#tpl-eait').html()
        });

        var id = $(this).attr('data-id');
        $.ajax({
            method: 'GET',
            url: 'http://api-breakingnews-web.itheima.net/my/article/cates/' + id,
            headers: {
                Authorization: localStorage.getItem('token')
            },
            success: function(res) {
                if (res.status !== 0) {
                    return layer.msg('获取文章分类数据失败！')
                }
                form.val('form-edit', res.data)
            }
        })
    })


    $('body').on('submit', '#form-edit', function(e) {
        e.preventDefault()
        $.ajax({
            method: 'POST',
            url: 'http://api-breakingnews-web.itheima.net/my/article/updatecate',
            data: $(this).serialize(),
            headers: {
                Authorization: localStorage.getItem('token')
            },
            success: function(res) {
                if (res.status !== 0) {
                    return layer.msg('更新分类信息失败！')
                }
                initArtCateList()
                layer.msg('更新分类信息成功！')
                layer.close(indexEdit)

            }

        })
    })

    // 删除按钮
    $('body').on('click', '#btnDelete', function() {
        var id = $(this).attr('data-del')
        layer.confirm('是否要删除', { icon: 3, title: '提示' }, function(index) {
            $.ajax({
                method: 'GET',
                url: 'http://api-breakingnews-web.itheima.net/my/article/deletecate/' + id,
                headers: {
                    Authorization: localStorage.getItem('token')
                },
                success: function(res) {
                    if (res.status !== 0) {
                        return layer.msg('删除文章分类失败！')
                    }
                    initArtCateList()
                    layer.msg('删除文章分类成功！')
                    layer.close(index);
                }
            })
        });
    })
})